package com.chinatechstar.file.controller;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.chinatechstar.file.entity.SysFileBusiness;
import com.chinatechstar.file.service.SysFileBusinessService;
import com.chinatechstar.component.commons.result.ActionResult;
import com.chinatechstar.component.commons.result.ListResult;
import com.chinatechstar.component.commons.result.ResultBuilder;
import com.chinatechstar.component.commons.utils.ExcelUtils;
import com.chinatechstar.component.commons.validator.InsertValidator;
import com.chinatechstar.component.commons.validator.UpdateValidator;

/**
 * 文件业务关联 的控制层
 */
@Api(value = "文件业务关联 的控制层", tags = {"文件业务关联"})
@RestController
@RequestMapping("/sysfilebusiness")
public class SysFileBusinessController {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private SysFileBusinessService sysFileBusinessService;

    /**
     * 查询 文件业务关联 所有数据
     *
     * @return ListResult<Object>
     */
    @ApiOperation(value = "查询 文件业务关联 所有数据", tags = {"文件业务关联"})
    @GetMapping(path = "/findSysFileBusinessAll")
    public ListResult<Object> findSysFileBusinessAll() {
        List<SysFileBusiness> list = sysFileBusinessService.findSysFileBusinessAll();
        return ResultBuilder.buildListSuccess(list);
    }

    /**
     * 查询 文件业务关联 分页
     *
     * @param currentPage 当前页数
     * @param pageSize    每页记录数
     * @param name        名称
     * @param sorter      排序
     * @return ListResult<Object>
     */
    @ApiOperation(value = "查询 文件业务关联 分页", tags = {"文件业务关联"})
    @GetMapping(path = "/findSysFileBusinessByCondition")
    public ListResult<Object> findSysFileBusinessByCondition(@RequestParam(name = "currentPage", required = false, defaultValue = "1") Integer currentPage,
                                                             @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                                                             @RequestParam(name = "name", required = false) String name,
                                                             @RequestParam(name = "sorter", required = false) String sorter) {
        Map<String, Object> data = sysFileBusinessService.findSysFileBusinessByCondition(currentPage, pageSize, name, sorter);
        return ResultBuilder.buildListSuccess(data);
    }

    /**
     * 根据查询条件导出 文件业务关联
     *
     * @param response 响应对象
     * @param paramMap 参数Map
     */
    @ApiOperation(value = "根据查询条件导出 文件业务关联", tags = {"文件业务关联"})
    @PostMapping(path = "/exportSysFileBusiness")
    public void exportSysFileBusiness(HttpServletResponse response, @RequestParam Map<String, Object> paramMap) {
        try {
            List<String> headList = Arrays.asList("ID", "名称", "创建时间");
            List<LinkedHashMap<String, Object>> dataList = sysFileBusinessService.findSysFileBusinessForExcel(paramMap);
            ExcelUtils.exportExcel(headList, dataList, "管理", response);
        } catch (Exception e) {
            logger.warn(e.toString());
        }
    }

    /**
     * 根据主键ID查询 文件业务关联 数据
     *
     * @param id ID
     * @return ActionResult
     */
    @ApiOperation(value = "根据主键ID数据查询 文件业务关联 数据", tags = {"文件业务关联"})
    @GetMapping(path = "/findSysFileBusinessById")
    public ActionResult findSysFileBusinessById(@RequestParam(name = "id") Long id) {
        SysFileBusiness data = sysFileBusinessService.findSysFileBusinessById(id);
        return ResultBuilder.buildActionSuccess(data);
    }

    /**
     * 根据主键ID数组查询 文件业务关联 数据
     *
     * @param ids ID数组
     * @return ActionResult
     */
    @ApiOperation(value = "根据主键ID数组查询 文件业务关联 数据", tags = {"文件业务关联"})
    @GetMapping(path = "/findSysFileBusinessByIds")
    public ListResult<Object> findSysFileBusinessByIds(@RequestParam(name = "ids") Long[] ids) {
        List<SysFileBusiness> list = sysFileBusinessService.findSysFileBusinessByIds(ids);
        return ResultBuilder.buildListSuccess(list);
    }

    /**
     * 根据 fb_id 查询当前文件的工作流状态数据
     *
     * @param fbId 文件业务ID
     * @return ActionResult
     */
    @ApiOperation(value = "根据 fb_id 查询当前文件的工作流状态数据", tags = {"文件业务关联"})
    @GetMapping(path = "/findFileWorkflowStatusByBusinessId")
    public ActionResult findFileWorkflowStatusByBusinessId(@RequestParam(name = "fileBusiness") Long fbId) {
        LinkedHashMap<String, Object> data = sysFileBusinessService.findFileWorkflowStatusByBusinessId(fbId);
        return ResultBuilder.buildActionSuccess(data);
    }

    /**
     * 新增 文件业务关联
     *
     * @param sysFileBusiness 文件业务关联 实体
     * @return ActionResult
     */
    @ApiOperation(value = "新增 文件业务关联", tags = {"文件业务关联"})
    @PostMapping(path = "/addSysFileBusiness")
    public ActionResult addSysFileBusiness(@Validated(InsertValidator.class) @RequestBody SysFileBusiness sysFileBusiness) {
        sysFileBusinessService.insertSysFileBusiness(sysFileBusiness);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 批量新增 文件业务关联
     *
     * @param sysFileBusinesses 文件业务关联 实体数组
     * @return ActionResult
     */
    @ApiOperation(value = "批量新增 文件业务关联", tags = {"文件业务关联"})
    @PostMapping(path = "/addSysFileBusinesses")
    public ActionResult addSysFileBusinesses(@Validated(InsertValidator.class) @RequestBody List<SysFileBusiness> sysFileBusinesses) {
        sysFileBusinessService.insertSysFileBusinesses(sysFileBusinesses);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 编辑 文件业务关联
     *
     * @param sysFileBusiness 文件业务关联 实体
     * @return ActionResult
     */
    @ApiOperation(value = "编辑 文件业务关联", tags = {"文件业务关联"})
    @PutMapping(path = "/updateSysFileBusiness")
    public ActionResult updateSysFileBusiness(@Validated(UpdateValidator.class) @RequestBody SysFileBusiness sysFileBusiness) {
        sysFileBusinessService.updateSysFileBusiness(sysFileBusiness);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 批量编辑 文件业务关联
     *
     * @param sysFileBusinesses 文件业务关联 实体数组
     * @return ActionResult
     */
    @ApiOperation(value = "批量编辑 文件业务关联", tags = {"文件业务关联"})
    @PostMapping(path = "/updateSysFileBusinesss")
    public ActionResult updateSysFileBusinesss(@Validated(UpdateValidator.class) @RequestBody List<SysFileBusiness> sysFileBusinesses) {
        sysFileBusinessService.updateSysFileBusinesses(sysFileBusinesses);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 删除 文件业务关联
     *
     * @param id ID
     * @return ActionResult
     */
    @ApiOperation(value = "根据主键ID删除 文件业务关联", tags = {"文件业务关联"})
    @PostMapping(path = "/deleteSysFileBusinessById")
    public ActionResult deleteSysFileBusinessById(@RequestParam(name = "id") Long id) {
        sysFileBusinessService.deleteSysFileBusinessById(id);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 删除 文件业务关联
     *
     * @param ids ID数组
     * @return ActionResult
     */
    @ApiOperation(value = "根据主键ID数组批量删除 文件业务关联", tags = {"文件业务关联"})
    @PostMapping(path = "/deleteSysFileBusinessByIds")
    public ActionResult deleteSysFileBusinessByIds(@RequestParam(name = "id") Long[] ids) {
        sysFileBusinessService.deleteSysFileBusinessByIds(ids);
        return ResultBuilder.buildActionSuccess();
    }

}
